layui.define(['jquery'], function(exports) {
  'use strict';

  var $ = layui.jquery;
  var MODULE_NAME = 'more';
  var ClassName = {
    ICON     : 'layui-icon',
    ICON_DOWN: 'layui-icon-down',
    ICON_UP  : 'layui-icon-up',
    MORE     : 'layadmin-more',
    MORE_META: 'layadmin-more-meta',
    MORE_CTRL: 'layadmin-more-ctrl'
  };
  var More = function() {
    this.selector = null;
    this.$elem = null;
    this.SHOW_TEXT = '显示更多';
    this.HIDE_TEXT = '隐藏更多';
  };
  More.prototype.render = function(selector) {
    var _this = this;

    this.selector = selector;
    this.$elem = $(selector);

    this.$elem.each(function(index) {
      var $module = $(this);
      var $meta = $('<div class="' + ClassName.MORE_META + '" />');
      var $ctrl = $('<div class="' + ClassName.MORE_CTRL + '"><i class="' + ClassName.ICON + ' ' + ClassName.ICON_DOWN + '"></i><span>' + _this.SHOW_TEXT + '</span></div>');

      if ($module.attr('lay-more-key')) return;
      $module.addClass(ClassName.MORE).attr('lay-more-key', '@@' + index);
      $meta.append($module.children()).appendTo($module);
      $ctrl.appendTo($module).data('shown', false);

      $ctrl.on('click.' + MODULE_NAME, function() {
        var $this = $(this);
        var isShow = $this.data('shown');
        $meta[isShow ? 'slideUp' : 'slideDown'](function() {
          $this.data('shown', !isShow);
          $this.find('span').text(isShow ? _this.SHOW_TEXT : _this.HIDE_TEXT);
          $this.find('.' + ClassName.ICON)[ isShow ? 'removeClass' : 'addClass' ](ClassName.ICON_UP);
          !isShow && $(this).css('overflow', ''); // 去除为了让一些下拉框可以展示
        });
      });
    });
  };

  exports(MODULE_NAME, new More());
});
